********* Table A5 *********
clear all
set more off 
capture log close
 
* main path to data
global path "~/Dropbox/Paraguay/1_data/7_replication_dataverse"
capture cd "$path"	

*tables = analysis tables
global tables "$path/tables"

* main panel
use "$path/data/main.dta", clear

xtset id year
generate autocracy = (year<1989)


* identify observations extreme in centrality 
sum zdegree
gen extreme_degree1 = 1 if abs(zdegree)  > abs(r(sd))

sum zbetweenness
gen extreme_bet1 = 1 if abs(zbetweenness) > abs(r(sd))

sum zeigen
gen extreme_eigen1 = 1 if abs(zeigen) > abs(r(sd))



eststo clear

* Panel A: Eigenvector Centrality
foreach y in tmh_ha_log tmh_e {
    local i = 1
    foreach fe in "year id" "year id i.depa#i.year" "year id i.district_code#i.year" "year id i.district_code#i.year i.year#i.lastname_d" {
        reghdfe `y' i.autocracy##c.zeigen [aweight=p_weights] if sample_weight == 1 & extreme_eigen1 != 1, absorb(`fe') cluster(id)
        
        * Store fixed effects indicators
        estadd local DT = cond("`fe'"=="year id i.depa#i.year", "Yes", "No")
        estadd local MY = cond("`fe'"=="year id i.district_code#i.year" | "`fe'"=="year id i.district_code#i.year i.year#i.lastname_d", "Yes", "No")
        estadd local FM = cond("`fe'"=="year id i.district_code#i.year i.year#i.lastname_d", "Yes", "No")
        
        * Store mean of dependent variable
        summ `y' if e(sample)==1 
        estadd scalar meanDep = r(mean)
        
        local col = `i'
        if "`y'" == "tmh_e" local col = `i' + 4
        eststo eigen`col'
        local ++i
    }
}

* Panel B: Degree Centrality
foreach y in tmh_ha_log tmh_e {
    local i = 1
    foreach fe in "year id" "year id i.depa#i.year" "year id i.district_code#i.year" "year id i.district_code#i.year i.year#i.lastname_d" {
        reghdfe `y' i.autocracy##c.zdegree [aweight=p_weights] if sample_weight == 1 & extreme_degree1 != 1, absorb(`fe') cluster(id)
        
        * Store fixed effects indicators
        estadd local DT = cond("`fe'"=="year id i.depa#i.year", "Yes", "No")
        estadd local MY = cond("`fe'"=="year id i.district_code#i.year" | "`fe'"=="year id i.district_code#i.year i.year#i.lastname_d", "Yes", "No")
        estadd local FM = cond("`fe'"=="year id i.district_code#i.year i.year#i.lastname_d", "Yes", "No")
        
        * Store mean of dependent variable
        summ `y' if e(sample)==1 
        estadd scalar meanDep = r(mean)
        
        local col = `i'
        if "`y'" == "tmh_e" local col = `i' + 4
        eststo degree`col'
        local ++i
    }
}

* Panel C: Betweenness Centrality
foreach y in tmh_ha_log tmh_e {
    local i = 1
    foreach fe in "year id" "year id i.depa#i.year" "year id i.district_code#i.year" "year id i.district_code#i.year i.year#i.lastname_d" {
        reghdfe `y' i.autocracy##c.zbetweenness [aweight=p_weights] if sample_weight == 1 & extreme_bet1 != 1, absorb(`fe') cluster(id)
        
        * Store fixed effects indicators
        estadd local DT = cond("`fe'"=="year id i.depa#i.year", "Yes", "No")
        estadd local MY = cond("`fe'"=="year id i.district_code#i.year" | "`fe'"=="year id i.district_code#i.year i.year#i.lastname_d", "Yes", "No")
        estadd local FM = cond("`fe'"=="year id i.district_code#i.year i.year#i.lastname_d", "Yes", "No")
        
        * Store mean of dependent variable
        summ `y' if e(sample)==1 
        estadd scalar meanDep = r(mean)
        
        local col = `i'
        if "`y'" == "tmh_e" local col = `i' + 4
        eststo between`col'
        local ++i
    }
}

* Create table header
file open tableA5 using "$tables/tableA5.tex", write replace
file write tableA5 "\begin{tabular}{lcccclcccc} \hline" _n
file write tableA5 "& \multicolumn{4}{c}{Hectares (log)}  &  & \multicolumn{4}{c}{Land Grant (binary)} \\" _n  
file write tableA5 "[3pt] \cline{2-5} \cline{7-10}\\" _n
file write tableA5 " & (1)        & (2)        & (3)        & (4)       &  & (5)        & (6)        & (7)        & (8)      \\" _n  
file write tableA5 "\hline" _n
file write tableA5 "\multicolumn{10}{l}{(a) \textit{Eigenvector Centrality}}                                                                             \\" _n  
file close tableA5

* Append panel A results
esttab eigen* using "$tables/tableA5.tex", append fragment nonumber label style(tex) ///
    cells(b(star fmt(%12.4fc)) se(par fmt(%12.4fc))) ///
    keep(1.autocracy#c.zeigen) ///
    stats(r2 meanDep N, fmt(%12.4fc %12.4fc %12.0fc ) ///
    labels("\$R^2\$" "Outcome mean" "Observations" )) ///
    varlabels(1.autocracy#c.zeigen "Autocracy \$\times\$ Eigenvector" ) ///
    extracols(5) collabels(none) mlabels(none) eqlabels(none) starlevels(* 0.1 ** 0.05 *** 0.01) ///
    noobs nonotes noline prehead("") posthead("") prefoot("") postfoot("")

* Add panel B header
file open tableA5 using "$tables/tableA5.tex", write append
file write tableA5 "\hline" _n
file write tableA5 "\multicolumn{10}{l}{(b) \textit{Degree Centrality}}   \\" _n  
file close tableA5

* Append panel B results
esttab degree* using "$tables/tableA5.tex", append fragment nonumber label style(tex) ///
    cells(b(star fmt(%12.4fc)) se(par fmt(%12.4fc))) ///
    keep(1.autocracy#c.zdegree) ///
    stats(r2 meanDep N, fmt(%12.4fc %12.4fc %12.0fc) ///
    labels("\$R^2\$" "Outcome mean" "Observations" )) ///
    varlabels(1.autocracy#c.zdegree "Autocracy \$\times\$ Degree" ) ///
    extracols(5) collabels(none) mlabels(none) eqlabels(none) starlevels(* 0.1 ** 0.05 *** 0.01) ///
    noobs nonotes noline prehead("") posthead("") prefoot("") postfoot("")

* Add panel C header
file open tableA5 using "$tables/tableA5.tex", write append
file write tableA5 "\hline" _n
file write tableA5 "\multicolumn{10}{l}{(c) \textit{Betweenness Centrality}}   \\" _n
file close tableA5

* Append panel C results with final statistics
esttab between* using "$tables/tableA5.tex", append fragment nonumber label style(tex) ///
    cells(b(star fmt(%12.4fc)) se(par fmt(%12.4fc))) ///
    keep(1.autocracy#c.zbetweenness) ///
    stats(r2 meanDep N DT MY FM, fmt(%12.4fc %12.4fc %12.0fc 0 0 0) ///
    labels("\$R^2\$" "Outcome mean" "Observations" "\hline Department-Year FE" "Municipality-Year FE" "Family-Year FE")) ///
    varlabels(1.autocracy#c.zbetweenness "Autocracy \$\times\$ Betweenness" ) ///
    extracols(5) collabels(none) mlabels(none) eqlabels(none) starlevels(* 0.1 ** 0.05 *** 0.01) ///
    noobs nonotes noline prehead("") posthead("") prefoot("") postfoot("\hline \end{tabular}")
